package com.algorithm.gcd;

/**
 * Created with IntelliJ IDEA.
 * User: LK
 * Date: 2018/4/13
 * Time: 17:25
 * To change this template use File | Settings | File Templates.
 * Description: 求两个数的最大公约数
 */
public class GCD {

    /**
     * @param a
     * @param b
     * @return a和b的最大公约数
     */
    public int gcd(int a, int b) {
        return b == 0 ? a : gcd(b,a%b);
    }

    /**
     * @param a
     * @param b
     * @return a和b的最小公倍数(最小公倍数为两数的乘积除以最大公约数。)
     */
    public int lcm(int a, int b) {
        return a * b / gcd(a, b);
    }

    public static void main(String[] args) {
        GCD gcd = new GCD();
        int a = 66;
        int b = 18;
        int result = gcd.gcd(a, b);
        System.out.println(a+"和"+b+"最大公约数是"+result);
        int lcmResult = gcd.lcm(a, b);
        System.out.println(a+"和"+b+"最小公倍数是"+lcmResult);
    }
}
